using System;
using System.Collections.Generic;
using System.Linq;
using GZ.Projects.WCS_SVC;

namespace GZ.Projects.WCS_SVC
{
	/// <summary>
	/// 货位转换帮助类
	/// 用于WMS、AGV与WCS货位之间的统一转换
	/// 支持双向查找，如RKST_01 ↔ 101005 ↔ AGV001
	/// </summary>
	public static class StationConvertHelper
	{
		#region 私有字段

		/// <summary>
		/// 站台映射信息
		/// </summary>
		private class StationMapping
		{
			public string WcsCode { get; set; }
			public string WmsCode { get; set; }
			public string AgvCode { get; set; }
		}

		/// <summary>
		/// 统一的站台映射配置
		/// 所有转换关系都基于这个配置生成，确保数据一致性
		/// </summary>
		private static readonly List<StationMapping> _stationMappings = new List<StationMapping>
		{
			new StationMapping { WcsCode = "101010", WmsCode = "RK01-01"},
			new StationMapping { WcsCode = "101012", WmsCode = "CK01-01"},
			new StationMapping { WcsCode = "101009", WmsCode = "RK01-02"},
			new StationMapping { WcsCode = "101001", WmsCode = "CK01-02"},
		};
		
		//创建一个字典用于stnNo和agvCode的相互映射查找，并提供两个方法使用

		/// <summary>
		/// WMS到WCS的转换字典
		/// </summary>
		private static readonly Dictionary<string, string> _wmsToWcsDict = CreateWmsToWcsDict();

		/// <summary>
		/// WCS到WMS的转换字典
		/// </summary>
		private static readonly Dictionary<string, string> _wcsToWmsDict = CreateWcsToWmsDict();

		/// <summary>
		/// AGV到WCS的转换字典
		/// </summary>
		private static readonly Dictionary<string, string> _agvToWcsDict = CreateAgvToWcsDict();

		/// <summary>
		/// WCS到AGV的转换字典
		/// </summary>
		private static readonly Dictionary<string, string> _wcsToAgvDict = CreateWcsToAgvDict();

		/// <summary>
		/// WMS到AGV的转换字典
		/// </summary>
		private static readonly Dictionary<string, string> _wmsToAgvDict = CreateWmsToAgvDict();

		/// <summary>
		/// AGV到WMS的转换字典
		/// </summary>
		private static readonly Dictionary<string, string> _agvToWmsDict = CreateAgvToWmsDict();

		#region 字典创建方法

		private static Dictionary<string, string> CreateWmsToWcsDict()
		{
			var dict = new Dictionary<string, string>();
			foreach (var mapping in _stationMappings)
			{
				if (!string.IsNullOrEmpty(mapping.WmsCode) && !string.IsNullOrEmpty(mapping.WcsCode))
				{
					dict[mapping.WmsCode] = mapping.WcsCode;
				}
			}
			return dict;
		}

		private static Dictionary<string, string> CreateWcsToWmsDict()
		{
			var dict = new Dictionary<string, string>();
			foreach (var mapping in _stationMappings)
			{
				if (!string.IsNullOrEmpty(mapping.WcsCode) && !string.IsNullOrEmpty(mapping.WmsCode))
				{
					dict[mapping.WcsCode] = mapping.WmsCode;
				}
			}
			return dict;
		}

		private static Dictionary<string, string> CreateAgvToWcsDict()
		{
			var dict = new Dictionary<string, string>();
			foreach (var mapping in _stationMappings)
			{
				if (!string.IsNullOrEmpty(mapping.AgvCode) && !string.IsNullOrEmpty(mapping.WcsCode))
				{
					dict[mapping.AgvCode] = mapping.WcsCode;
				}
			}
			return dict;
		}

		private static Dictionary<string, string> CreateWcsToAgvDict()
		{
			var dict = new Dictionary<string, string>();
			foreach (var mapping in _stationMappings)
			{
				if (!string.IsNullOrEmpty(mapping.WcsCode) && !string.IsNullOrEmpty(mapping.AgvCode))
				{
					dict[mapping.WcsCode] = mapping.AgvCode;
				}
			}
			return dict;
		}

		private static Dictionary<string, string> CreateWmsToAgvDict()
		{
			var dict = new Dictionary<string, string>();
			foreach (var mapping in _stationMappings)
			{
				if (!string.IsNullOrEmpty(mapping.WmsCode) && !string.IsNullOrEmpty(mapping.AgvCode))
				{
					dict[mapping.WmsCode] = mapping.AgvCode;
				}
			}
			return dict;
		}

		private static Dictionary<string, string> CreateAgvToWmsDict()
		{
			var dict = new Dictionary<string, string>();
			foreach (var mapping in _stationMappings)
			{
				if (!string.IsNullOrEmpty(mapping.AgvCode) && !string.IsNullOrEmpty(mapping.WmsCode))
				{
					dict[mapping.AgvCode] = mapping.WmsCode;
				}
			}
			return dict;
		}

		#endregion

		#endregion

		#region 公共方法

		#region WMS ↔ WCS 转换

		/// <summary>
		/// 将WMS货位转换为WCS货位
		/// 如果是LXK-、TPK-格式则不转换，直接返回原值
		/// </summary>
		/// <param name="wmsLocation">WMS货位编号</param>
		/// <returns>转换后的WCS货位编号，如果不需要转换或转换失败返回原值</returns>
		public static string ConvertWmsToWcs(string wmsLocation)
		{
			if (string.IsNullOrEmpty(wmsLocation))
			{
				return wmsLocation;
			}

			// 如果是LXK-、TPK-格式，不转换直接返回
			if (wmsLocation.StartsWith("YBLK-"))
			{
				return wmsLocation;
			}

			// 尝试从字典转换
			if (_wmsToWcsDict.TryGetValue(wmsLocation, out string wcsLocation))
			{
				return wcsLocation;
			}

			// 如果字典中没有找到，返回原值
			return wmsLocation;
		}

		/// <summary>
		/// 将WCS货位转换为WMS货位
		/// </summary>
		/// <param name="wcsLocation">WCS货位编号</param>
		/// <returns>转换后的WMS货位编号，如果转换失败返回原值</returns>
		public static string ConvertWcsToWms(string wcsLocation)
		{
			if (string.IsNullOrEmpty(wcsLocation))
			{
				return wcsLocation;
			}

			// 尝试从字典转换
			if (_wcsToWmsDict.TryGetValue(wcsLocation, out string wmsLocation))
			{
				return wmsLocation;
			}

			// 如果字典中没有找到，返回原值
			return wcsLocation;
		}

		#endregion

		#region AGV ↔ WCS 转换

		/// <summary>
		/// 将AGV货位转换为WCS货位
		/// </summary>
		/// <param name="agvLocation">AGV货位编号</param>
		/// <returns>转换后的WCS货位编号，如果转换失败返回原值</returns>
		public static string ConvertAgvToWcs(string agvLocation)
		{
			if (string.IsNullOrEmpty(agvLocation))
			{
				return agvLocation;
			}

			// 尝试从字典转换
			if (_agvToWcsDict.TryGetValue(agvLocation, out string wcsLocation))
			{
				return wcsLocation;
			}

			// 如果字典中没有找到，返回原值
			return agvLocation;
		}

		/// <summary>
		/// 将WCS货位转换为AGV货位
		/// </summary>
		/// <param name="wcsLocation">WCS货位编号</param>
		/// <returns>转换后的AGV货位编号，如果转换失败返回原值</returns>
		public static string ConvertWcsToAgv(string wcsLocation)
		{
			if (string.IsNullOrEmpty(wcsLocation))
			{
				return wcsLocation;
			}

			// 尝试从字典转换
			if (_wcsToAgvDict.TryGetValue(wcsLocation, out string agvLocation))
			{
				return agvLocation;
			}

			// 如果字典中没有找到，返回原值
			return wcsLocation;
		}

		#endregion

		#region WMS ↔ AGV 转换

		/// <summary>
		/// 将WMS货位转换为AGV货位
		/// 如果是LXK-、TPK-格式则不转换，直接返回原值
		/// </summary>
		/// <param name="wmsLocation">WMS货位编号</param>
		/// <returns>转换后的AGV货位编号，如果不需要转换或转换失败返回原值</returns>
		public static string ConvertWmsToAgv(string wmsLocation)
		{
			if (string.IsNullOrEmpty(wmsLocation))
			{
				return wmsLocation;
			}

			// 如果是LXK-、TPK- YBLK- 格式，不转换直接返回
			if (wmsLocation.StartsWith("YBLK-"))
			{
				return wmsLocation;
			}

			// 尝试从字典转换
			if (_wmsToAgvDict.TryGetValue(wmsLocation, out string agvLocation))
			{
				return agvLocation;
			}

			// 如果字典中没有找到，返回原值
			return wmsLocation;
		}

		/// <summary>
		/// 将AGV货位转换为WMS货位
		/// </summary>
		/// <param name="agvLocation">AGV货位编号</param>
		/// <returns>转换后的WMS货位编号，如果转换失败返回原值</returns>
		public static string ConvertAgvToWms(string agvLocation)
		{
			if (string.IsNullOrEmpty(agvLocation))
			{
				return agvLocation;
			}

			// 尝试从字典转换
			if (_agvToWmsDict.TryGetValue(agvLocation, out string wmsLocation))
			{
				return wmsLocation;
			}

			// 如果字典中没有找到，返回原值
			return agvLocation;
		}

		#endregion

		#region 通用转换方法

		/// <summary>
		/// 通用的站台转换方法
		/// 自动识别输入类型并转换为目标类型
		/// </summary>
		/// <param name="sourceLocation">源货位编号</param>
		/// <param name="targetType">目标类型：WCS、WMS、AGV</param>
		/// <returns>转换后的货位编号</returns>
		public static string ConvertStation(string sourceLocation, string targetType)
		{
			if (string.IsNullOrEmpty(sourceLocation) || string.IsNullOrEmpty(targetType))
			{
				return sourceLocation;
			}

			targetType = targetType.ToUpper();

			// 如果是LXK-、TPK-格式且目标不是WCS，不转换直接返回
			if ((sourceLocation.StartsWith("LXK-") || sourceLocation.StartsWith("TPK-")) && targetType != "WCS")
			{
				return sourceLocation;
			}

			switch (targetType)
			{
				case "WCS":
					// 尝试从WMS转换
					if (_wmsToWcsDict.ContainsKey(sourceLocation))
						return ConvertWmsToWcs(sourceLocation);
					// 尝试从AGV转换
					if (_agvToWcsDict.ContainsKey(sourceLocation))
						return ConvertAgvToWcs(sourceLocation);
					break;

				case "WMS":
					// 尝试从WCS转换
					if (_wcsToWmsDict.ContainsKey(sourceLocation))
						return ConvertWcsToWms(sourceLocation);
					// 尝试从AGV转换
					if (_agvToWmsDict.ContainsKey(sourceLocation))
						return ConvertAgvToWms(sourceLocation);
					break;

				case "AGV":
					// 尝试从WCS转换
					if (_wcsToAgvDict.ContainsKey(sourceLocation))
						return ConvertWcsToAgv(sourceLocation);
					// 尝试从WMS转换
					if (_wmsToAgvDict.ContainsKey(sourceLocation))
						return ConvertWmsToAgv(sourceLocation);
					break;
			}

			// 如果没有找到对应的转换，返回原值
			return sourceLocation;
		}

		#endregion
		
		#region 获取任务站台

		/// <summary>
		/// 获取任务站台
		/// </summary>
		/// <param name="fromPos">源货位</param>
		/// <param name="toPos">目标类型</param>
		/// <param name="posCode">编号</param>
		/// <returns>转换后的编号</returns>
		public static string GetCurWcsPos(string fromPos, string toPos,string posCode)
		{
			string stnNo = "";
			switch (posCode)
			{
					case "[FROM_POS]" : stnNo = fromPos; break;
				case "[FROM_POS_1]":
					stnNo = fromPos + "_1";
					break;
				case "[FROM_POS_2]":
					stnNo = fromPos + "_2";
					break;
				case "[FROM_POS_LEAVE]":
					stnNo = fromPos + "_LEAVE";
					break;
				case "[TO_POS]":
					stnNo = toPos;
					break;
				case "[TO_POS_1]":
					stnNo = toPos + "_1";
					break;
									case "[TO_POS_2]":
					stnNo = toPos + "_2";
					break;
				case "[TO_POS_LEAVE]":
					stnNo = toPos + "_LEAVE";
					break;
				case "[TO_POS_END]":
					stnNo = toPos + "_END";
					break;
					default: stnNo = posCode; break;
			}
			return stnNo;
		}

		public static string GetCurWcsPosToAgvPos(string fromPos, string toPos,string posCode)
		{
			string wcsPos = GetCurWcsPos(fromPos, toPos, posCode);
			if(!string.IsNullOrEmpty(wcsPos))
				return ConvertWcsToAgv(wcsPos);
			return wcsPos;
		}
		#endregion

		#endregion
	}
}